'''
Author: xiaonancheng
Date: 2023-07-03 11:45:57
https://www.cnblogs.com/xiaonancheng
Copyright (c) 2023 by @xiaonancheng, All Rights Reserved. 
'''
from django.shortcuts import render,redirect
from django.http import HttpResponse,JsonResponse
from UserAPP.models import User,Box
# Create your views here.

def add_user(request):
    #####增加的方式一####
    # user=User() #实例化对象
    # user.username='孙龙'#修改属性值
    # user.password='1234567890'#
    # user.save()#保存数据 （同步更新到数据库）
    #####增加的方式一####

    #####增加的方式二####
    # user=User(username='孙龙',password='1234567890')
    # user.save()

    #####增加的方式二####

    #####查询一个####
    # user=User.objects.get(uid=1)
    # # sql :select * from user where uid=1 limite 0,1 
    # print(user.username,user.password)
    #####查询一个####

    #####查询多个 方式一 ####
    # users=User.objects.filter(username='孙龙')#[:2]
    # for user in users:
        
    #     print(user.uid,user.username,user.password)
    #sql: select * from user where uid=1
    #####查询多个 方式一 ####

    #####查询多个 方式二 ####
    # users=User.objects.all()
    # # sql select * from user
    # for user in users:
        
    #     print(user.uid,user.username,user.password)

    #####查询多个 方式二 ####

    ##### 查询的其他方法 排序 #####
    # users=User.objects.all().filter(username='孙龙').order_by('-uid')
    # for user in users:
    #     print(user.uid,user.username,user.password)
    ##### 查询的其他方法 排序 #####

    ##### 修改 ######
    # user=User.objects.get(pk=1)#uid=1 等价
    # user.username='邵青'
    # user.save()
    ##### 修改 ######

    ##### 删除 ######
    user=User.objects.get(pk=4)#uid=1 等价
    user.delete()
    ##### 删除 ######




    return JsonResponse({'code':200})
 
def delate_user(request):
    pass
def get_user(request):
    pass
def get_all_user(request):
    pass
def update_user(request):
    pass

def login(request):# 登陆

    if request.method=="GET":
        return render(request,'user/login.html',)
    elif request.method=="POST":
        username=request.POST.get('username')
        password=request.POST.get('password')
        if not username:
            # 不填用户名 直接提交 "" None
            return render(request,'user/login.html',{'msg':'用户不能为空'})
        users=User.objects.filter(username=username)
        if not users:
            # 用户名 不存在
            return render(request,'user/login.html',{'msg':'用户不存在'})
        user=users[0]
        if user.password != password:
            # 密码错误
            return render(request,'user/login.html',{'msg':'密码错误','username':username})
        # 一切通过 登陆成功 
        request.session['is_login']=True# 是否登陆
        request.session['uid']=user.uid
        request.session['username']=user.username


        return redirect('/')#到首页
    else:
        return JsonResponse({'msg':'非法请求'})
def register(request):#注册
    if request.method=="GET":
        return render(request,'user/register.html')
    elif request.method=="POST":
        username=request.POST.get('username')
        pw1=request.POST.get('pw1')
        pw2=request.POST.get('pw2')
        if not username:
            # 不填用户名 直接提交 "" None
            return render(request,'user/register.html',{'msg':'用户不能为空'})
        users=User.objects.filter(username=username)
        if users:
            return render(request,'user/register.html',{'msg':'用户名已存在请重新输入'})
        if pw1!=pw2:
            return render(request,'user/register.html',{'msg':'两次密码不一致'})
        user=User(username=username,password=pw2)
        user.save()
        return redirect('UserAPP:login')
def logout(request):#登出
    """
    点击登出 返回 登陆页面 或者 首页 
    验证：是否在线 

    """
    if request.session['is_login']:
        # 方法一 清除 session
        request.session['is_login']=False
        request.session['uid']=''
        request.session['username']=''
        # 方法一 清除 session

        # 方法二 清除 session
        # request.session.clear()
        # 方法二 清除 session
    return redirect('/')